﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AnimalCare.Model;

namespace AnimalCare.Controller
{
    class CustomerService
    {
        public static void Insert(string customerName, string customerPhone, string customerAddress , string customerInfo)
        {
            Customer cf = new Customer();
            cf.customer_name = customerName;
            cf.customer_phone = customerPhone;
            cf.customer_address = customerAddress;
            cf.customer_info = customerInfo;
            using (var ctx = new AnimalCareDBEntities())
            {
                ctx.Customers.AddObject(cf);
                ctx.SaveChanges();
            }
        }

        public static void Update(Customer cf)
        {
            using (var ctx = new AnimalCareDBEntities())
            {
                var customer = (from s in ctx.Customers
                                where s.customer_id == cf.customer_id
                                select s).FirstOrDefault();
                customer.customer_name = cf.customer_name;
                customer.customer_phone = cf.customer_phone;
                customer.customer_address = cf.customer_address;
                customer.customer_info = cf.customer_info;
                int num = ctx.SaveChanges();
            }
        }

        public static void Delete(int id)
        {
            try
            {
                using (var ctx = new AnimalCareDBEntities())
                {
                    var cf = (from s in ctx.Customers
                              where s.customer_id == id
                              select s).FirstOrDefault();
                    ctx.Customers.DeleteObject(cf);
                    int num = ctx.SaveChanges();
                }
            }
            catch
            {
            }
        }
        public static List<Customer> search(string name)
        {
            List<Customer> customer = new List<Customer>();
            using (var ctx = new AnimalCareDBEntities())
            {
                var C = (from s in ctx.Customers
                         where s.customer_name.Contains(name)
                         select s);
                customer = C.ToList();
            }
            return customer;
        }
    }
}
